import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter } from '@angular/core';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { Http } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';

import { Service } from '../../../server/server';
import { Pagination } from '../../../public/serve/pcrudInfo';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
declare var $:any;

interface Response {
  result: string[];
  result_code:string;
  message:string;
  length:number;
}

@Component({
  selector: 'app-task-table',
  templateUrl: './task-table.component.html'
})
export class TaskTableComponent implements OnInit {

  @Input() tableNumber : number;
  
  right: any = { "system" : {}, "task" : {} };
  constructor(private server: Service, private router: Router, private tableListService: CudTableService, private http: HttpClient,private removeTableService: RemoveTableService) {
    this.right = $.extend(this.right, server.getRight());
  }

  ngOnInit() {
    this.getTabList(1, 20, "0",2,this.taskName);

    //接受查询结果
    // this.server.tasksearcheventEmit.subscribe((value: any) => {
    //   this.taskName = value;
    //   console.log(this.taskName)
    //   this.getTabList(1, 20, "0",this.taskName);
    // });
  }
  //查询条件
  public taskName: any = '';

  public tabVal:any;
  
  @Output()
  tabeListOutput: EventEmitter<string> = new EventEmitter();
  // 表格
  getTabList(pageNumber, pageSize, id,queryType, taskName) {
    let url = this.server.baseUrl + 'admin/tasks/list';
    let data = { id: id, pageNo: pageNumber, pageSize: pageSize, queryType:queryType, queryCriteria:taskName}
    //console.log(data);
    this.tableListService.postRequest(url, data).then((data: any) => {
      //console.log(data);
      this.tabVal = data.result;
      this.tabeListOutput.emit(data.length);
      //console.log(data.length);
    }).catch();
  }


  //详情
  details(val) {
    let url = this.server.baseUrl + 'admin/tasks/info';
    let data = { taskId: val.taskId };
    //console.log(data);
    this.http.post(url, data).subscribe((res: any) => {
      this.server.taskdetailseventEmit.emit(res.result);
      //console.log(res.result);
    })
  }

  // 全选
  public isSelectAll: boolean = false;
  public isChose: boolean = false;
  public isSelect: boolean = true;
  public isDisable: any;              // 按钮（启用/禁用）

  @Output() isSelectFromTable = new EventEmitter<boolean>();

  selectAll(bool, tabVal) {
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'taskId');
    this.isSelect = !this.isSelectAll;
    this.isSelectFromTable.emit(this.isSelect);
    //console.log(this.removeTableService.getRemoveTableId)
  }

  // 单选（多选）
  chose(bool, taskId, length) {
    this.isDisable = this.removeTableService.removeChose(bool, taskId, length);
    this.isSelectAll = this.isDisable.bool;
    this.isDisable.len ? this.isSelect = false : this.isSelect = true;
    this.isSelectFromTable.emit(this.isSelect);
    //console.log(this.removeTableService.getRemoveTableId)
  }

  // 没有数据时取消勾选全选按钮
  cancelSelectAll() {
    this.isSelectAll = false;
    this.isChose = false;
  }
}